package com.sls.game.service.impl;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.http.HttpClient;
import com.sls.core.socket.Head;
import com.sls.core.socket.ServerDisponseException;
import com.sls.core.util.ServerDateAndTime;
import com.sls.core.util.UtilMD5Encryption;
import com.sls.game.assist.Helper;
import com.sls.game.common.DeductionRefund;
import com.sls.game.constant.ExceptionCodeDes;
import com.sls.game.dao.DownstreamUserDAO;
import com.sls.game.dao.GoodsOrderDAO;
import com.sls.game.dao.GoodsOrderLogDAO;
import com.sls.game.dao.OhFeiGoodsAreaServerDAO;
import com.sls.game.dao.OhFeiGoodsBrandDAO;
import com.sls.game.dao.OhFeiGoodsInfoDAO;
import com.sls.game.page.Pagination;
import com.sls.game.parser.ohfei.OhFeiAssembleHttpUrl;
import com.sls.game.parser.ohfei.OhFeiResOfBalanceSaxParseXml;
import com.sls.game.parser.ohfei.OhFeiResOfDomParseXml;
import com.sls.game.parser.ohfei.OhFeiServerSaxParseXml;
import com.sls.game.parser.sls.OrderStatusSendParse;
import com.sls.game.pojo.DeductionRefundInfo;
import com.sls.game.pojo.DownstreamUserInfo;
import com.sls.game.pojo.GoodsOrderInfo;
import com.sls.game.pojo.GoodsOrderLog;
import com.sls.game.pojo.OhFeiGoodsAreaServerInfo;
import com.sls.game.pojo.OhFeiGoodsBrandInfo;
import com.sls.game.pojo.OhFeiGoodsInfo;
import com.sls.game.pojo.OhFeiOrderStatusInfo;
import com.sls.game.pojo.OhFeiResponseInfo;
import com.sls.game.pojo.OhFeiServerInfo;
import com.sls.game.pojo.OhFeiTradeInfo;
import com.sls.game.pojo.SLSHttpInterfaceInfo;
import com.sls.game.service.OhFeiGoodsService;

/**
 * 
 * @description 殴飞相关管理接口
 * @author Wangxuegang
 * @date 2012-9-20
 * 
 */
@Service("ohFeiGoodsService")
public class OhFeiGoodsServiceImpl implements OhFeiGoodsService {

	private static Logger log = Logger.getLogger(OhFeiGoodsServiceImpl.class);

	private Gson gson = null;

	public OhFeiGoodsServiceImpl() {
		gson = CommonUtil.gson();
	}

	private OhFeiGoodsBrandDAO ohFeiGoodsBrandDAO;

	private OhFeiGoodsInfoDAO ohFeiGoodsInfoDAO;

	private OhFeiGoodsAreaServerDAO ohFeiGoodsAreaServerDAO;

	/**
	 * 网游充值订单表DAO
	 */
	private GoodsOrderDAO goodsOrderDAO;

	private GoodsOrderLogDAO goodsOrderLogDAO;

	private DownstreamUserDAO downstreamUserDAO;

	public DownstreamUserDAO getDownstreamUserDAO() {
		return downstreamUserDAO;
	}

	@Resource(name = "downstreamUserDAO")
	public void setDownstreamUserDAO(DownstreamUserDAO downstreamUserDAO) {
		this.downstreamUserDAO = downstreamUserDAO;
	}

	public GoodsOrderLogDAO getGoodsOrderLogDAO() {
		return goodsOrderLogDAO;
	}

	@Resource(name = "goodsOrderLogDAO")
	public void setGoodsOrderLogDAO(GoodsOrderLogDAO goodsOrderLogDAO) {
		this.goodsOrderLogDAO = goodsOrderLogDAO;
	}

	public GoodsOrderDAO getGoodsOrderDAO() {
		return goodsOrderDAO;
	}

	@Resource(name = "goodsOrderDAO")
	public void setGoodsOrderDAO(GoodsOrderDAO goodsOrderDAO) {
		this.goodsOrderDAO = goodsOrderDAO;
	}

	public OhFeiGoodsAreaServerDAO getOhFeiGoodsAreaServerDAO() {
		return ohFeiGoodsAreaServerDAO;
	}

	@Resource(name = "ohFeiGoodsAreaServerDAO")
	public void setOhFeiGoodsAreaServerDAO(
			OhFeiGoodsAreaServerDAO ohFeiGoodsAreaServerDAO) {
		this.ohFeiGoodsAreaServerDAO = ohFeiGoodsAreaServerDAO;
	}

	public OhFeiGoodsInfoDAO getOhFeiGoodsInfoDAO() {
		return ohFeiGoodsInfoDAO;
	}

	@Resource(name = "ohFeiGoodsInfoDAO")
	public void setOhFeiGoodsInfoDAO(OhFeiGoodsInfoDAO ohFeiGoodsInfoDAO) {
		this.ohFeiGoodsInfoDAO = ohFeiGoodsInfoDAO;
	}

	public OhFeiGoodsBrandDAO getOhFeiGoodsBrandDAO() {
		return ohFeiGoodsBrandDAO;
	}

	@Resource(name = "ohFeiGoodsBrandDAO")
	public void setOhFeiGoodsBrandDAO(OhFeiGoodsBrandDAO ohFeiGoodsBrandDAO) {
		this.ohFeiGoodsBrandDAO = ohFeiGoodsBrandDAO;
	}

	/**
	 * 手拉手在殴飞账号余额查询
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String queryBalanceOfOhFei(String body, Head head)
			throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		// 对消息body进行解析
		OhFeiGoodsInfo info = gson.fromJson(body, OhFeiGoodsInfo.class);
		boolean flag = checkDataOfBalance(info, head.getRequestId());
		if (flag) { // 数据校验通过
			try {
				// 所传参数字符串拼接
				String paraStr = assembleParaOfBalanceStr(info);
				// 余额查询的地址
				String path = "queryuserinfo.do";
				String url = OhFeiAssembleHttpUrl
						.assembleHttpUrl(paraStr, path);
				// 发送http请求
				OhFeiResponseInfo resInfo = sendHttpRequest(url);

				OhFeiGoodsInfo ohFeiGoodsInfo = new OhFeiGoodsInfo();
				if (resInfo != null) {
					String err_msg = resInfo.getErr_msg();
					if (StringUtils.isNotEmpty(err_msg)) {
						err_msg = URLDecoder.decode(err_msg, "GBK");
					}
					ohFeiGoodsInfo.setErr_msg(err_msg);
					ohFeiGoodsInfo.setRetcode(resInfo.getRetcode());
					ohFeiGoodsInfo.setRet_leftcredit(resInfo
							.getRet_leftcredit());
				}

				String resultBody = gson.toJson(ohFeiGoodsInfo);
				log.info("返回消息：  " + resultBody);
				if (log.isInfoEnabled()) {
					log.info("Service处理[" + head.getRequestId() + "]的请求已结束");
				}
				return resultBody;

			} catch (Exception e) {
				log.error("Service处理[" + head.getRequestId() + "]异常！");
				throw new ServerDisponseException("9", e.getMessage());
			}
		}

		return null;
	}

	/**
	 * 殴飞品牌新增同步
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String saveOhFeiBrand(String body, Head head)
			throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		// 对消息body进行解析
		OhFeiGoodsBrandInfo info = gson.fromJson(body,
				OhFeiGoodsBrandInfo.class);
		boolean flag = checkDataOfBrand(info, head.getRequestId());
		if (flag) { // 数据校验通过
			// 所传参数字符串拼接
			String paraStr = assembleParaOfBrandStr(info);
			// 品牌同步的地址
			String path = "querylist.do";
			String url = OhFeiAssembleHttpUrl.assembleHttpUrl(paraStr, path);
			// 发送http请求
			OhFeiResponseInfo resInfo = sendHttpRequestOfBrand(url, head);

			String resultBody = null;
			resultBody = gson.toJson(resInfo);

			log.info("分页查询结果为：  " + resultBody);
			if (log.isInfoEnabled()) {
				log.info("Service处理[" + head.getRequestId() + "]的请求已结束");
			}
			return resultBody;
		}

		return null;
	}

	/**
	 * 殴飞产品新增同步
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String saveOhFeiGoods(String body, Head head)
			throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		// 对消息body进行解析
		OhFeiGoodsInfo ohFeiGoodsInfo = gson.fromJson(body,
				OhFeiGoodsInfo.class);

		boolean flag = checkDataOfGoods(ohFeiGoodsInfo, head.getRequestId());
		if (flag) { // 数据校验通过
			String cardId = ohFeiGoodsInfo.getCardid();
			// 商品同步的地址
			String path = "querycardinfo.do";
			if (StringUtils.isEmpty(cardId)) {
				List<String> cardIdList = this.ohFeiGoodsBrandDAO
						.queryAllCardId();
				if (cardIdList != null && !cardIdList.isEmpty()) {
					int count = 0;
					for (String cardid : cardIdList) {
						ohFeiGoodsInfo.setCardid(cardid);
						// 所传参数字符串拼接
						String paraStr = assembleParaOfGoods(ohFeiGoodsInfo);

						String url = OhFeiAssembleHttpUrl.assembleHttpUrl(
								paraStr, path);
						// 发送http请求
						OhFeiGoodsInfo resInfo = sendHttpRequestOfGoods(url,
								head);
						String retCode = resInfo.getRetcode();
						if (StringUtils.isNotEmpty(retCode)
								&& !"1".equals(retCode)) {
							continue;
						} else {
							int num = resInfo.getResNum();
							count += num;
						}
					}
					OhFeiGoodsInfo resInfo2 = new OhFeiGoodsInfo();
					resInfo2.setResNum(count);
					resInfo2.setRetcode("1");
					return gson.toJson(resInfo2);
				}

			} else {

				// 所传参数字符串拼接
				String paraStr = assembleParaOfGoods(ohFeiGoodsInfo);
				String url = OhFeiAssembleHttpUrl
						.assembleHttpUrl(paraStr, path);
				// 发送http请求
				OhFeiGoodsInfo resInfo = sendHttpRequestOfGoods(url, head);

				String resultBody = null;
				resultBody = gson.toJson(resInfo);
				return resultBody;
			}
		}

		return null;
	}

	/**
	 * 殴飞产品区服新增同步
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String saveOhFeiAreaServer(String body, Head head)
			throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		// 对消息body进行解析
		OhFeiGoodsAreaServerInfo info = gson.fromJson(body,
				OhFeiGoodsAreaServerInfo.class);
		// 所传参数字符串拼接
		String paraStr = assembleParaOfAreaServer(info);
		// 商品同步的地址
		String path = "getareaserver.do";
		String url = OhFeiAssembleHttpUrl.assembleHttpUrl(paraStr, path);
		// 发送http请求
		List<OhFeiGoodsAreaServerInfo> resInfoList = sendHttpRequestOfAreaServer(url);

		// 把响应的结果插入数据库
		insertAreaServer(resInfoList, head);

		// 同步区服记录数
		int resNum = 0;
		String resultBody = null;
		resNum = resInfoList.size();
		resultBody = gson.toJson(resNum);

		log.info("分页查询结果为：  " + resultBody);
		if (log.isInfoEnabled()) {
			log.info("Service处理[" + head.getRequestId() + "]的请求已结束");
		}

		return resultBody;
	}

	/**
	 * 殴飞提取卡密信息（卡密充值交易）
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String saveExtractCardInfo(String body, Head head)
			throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		// 对消息body进行解析
		OhFeiTradeInfo info = gson.fromJson(body, OhFeiTradeInfo.class);
		boolean flag = checkDataOfSaveExtractCardInfo(info, head.getRequestId());

		if (flag) { // 数据校验通过
			// 所传参数字符串拼接
			String paraStr = assembleParaOfExtractCard(info);
			// 商品同步的地址
			String path = "order.do";
			String url = OhFeiAssembleHttpUrl.assembleHttpUrl(paraStr, path);
			// 发送http请求
			List<OhFeiTradeInfo> resInfoList = sendHttpRequestOfExtractCard(url);

			String resultBody = null;
			resultBody = gson.toJson(resInfoList);
			if (log.isInfoEnabled()) {
				log.info("Service处理[" + head.getRequestId() + "]的请求已结束");
			}
			return resultBody;
		}

		return null;
	}

	/**
	 * 殴飞游戏直充（订单交易）
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String saveDirectRecharge(String body, Head head)
			throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		// 对消息body进行解析
		OhFeiTradeInfo info = gson.fromJson(body, OhFeiTradeInfo.class);
		boolean flag = checkDataOfSaveDirectRecharge(info, head.getRequestId());
		if (flag) { // 数据校验通过
			// 所传参数字符串拼接
			String paraStr = assembleParaOfDirectRecharge(info);
			// 商品同步的地址
			String path = "onlineorder.do";
			String url = OhFeiAssembleHttpUrl.assembleHttpUrl(paraStr, path);
			// 发送http请求
			OhFeiTradeInfo resInfo = sendHttpRequestOfDirectRecharge(url);
			String resultBody = null;
			if (resInfo != null) {
				resultBody = gson.toJson(resInfo);
			}
			if (log.isInfoEnabled()) {
				log.info("Service处理[" + head.getRequestId() + "]的请求已结束");
			}
			return resultBody;
		}

		return null;
	}

	/**
	 * 殴飞商品品牌分页查询
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String queryPageInquire(String body, Head head)
			throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		// 对消息body进行解析
		OhFeiGoodsBrandInfo info = gson.fromJson(body,
				OhFeiGoodsBrandInfo.class);
		boolean flag = checkDataOfByPage(info, head.getRequestId());
		if (flag) {
			Pagination<OhFeiGoodsBrandInfo> paginationList = null;
			paginationList = ohFeiGoodsBrandDAO.queryPaginationList(info);
			String resultBody = null;
			if (paginationList != null) {
				resultBody = gson.toJson(paginationList);
			}

			log.info("分页查询结果为：  " + resultBody);
			if (log.isInfoEnabled()) {
				log.info("Service处理[" + head.getRequestId() + "]的请求已结束");
			}
			return resultBody;
		}

		return null;
	}

	/**
	 * 殴飞区服分页查询
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String queryAreaServerByPageInquire(String body, Head head)
			throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		// 对消息body进行解析
		OhFeiGoodsAreaServerInfo info = gson.fromJson(body,
				OhFeiGoodsAreaServerInfo.class);
		boolean flag = checkDataOfAreaServerByPage(info, head.getRequestId());
		if (flag) { // 数据校验通过
			Pagination<OhFeiGoodsAreaServerInfo> paginationList = null;
			paginationList = ohFeiGoodsAreaServerDAO.queryPaginationList(info);

			String resultBody = null;
			if (paginationList != null) {
				resultBody = gson.toJson(paginationList);
			}

			log.info("分页查询结果为：  " + resultBody);
			if (log.isInfoEnabled()) {
				log.info("Service处理[" + head.getRequestId() + "]的请求已结束");
			}
			return resultBody;

		}

		return null;
	}

	/**
	 * 数据校验
	 */
	private boolean checkDataOfAreaServerByPage(OhFeiGoodsAreaServerInfo info,
			String reqId) throws ServerDisponseException {
		boolean flag = false;
		String currentpage = info.getCurrentPage() + "";
		String pageSize = info.getPageSize() + "";
		if ("".equals(currentpage) || currentpage == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.CURRENT_PAGE_NULL.getCode(),
					ExceptionCodeDes.CURRENT_PAGE_NULL.getDescription());
		} else if ("".equals(pageSize) || pageSize == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.PAGE_SIZE_NULL.getCode(),
					ExceptionCodeDes.PAGE_SIZE_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	/**
	 * 数据校验
	 */
	private boolean checkDataOfSaveExtractCardInfo(OhFeiTradeInfo info,
			String reqId) throws ServerDisponseException {
		boolean flag = false;
		String userId = info.getUserid();
		String userPws = info.getUserpws();
		String cardId = info.getCardid();
		String cardNum = info.getCardnum();
		String sporder_id = info.getSporder_id();
		String sporder_time = info.getSporder_time();

		if ("".equals(userId) || userId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.COOPERATION_USERNO_NULL.getCode(),
					ExceptionCodeDes.COOPERATION_USERNO_NULL.getDescription());
		} else if ("".equals(userPws) || userPws == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.USER_PASSWORD_NULL.getCode(),
					ExceptionCodeDes.USER_PASSWORD_NULL.getDescription());
		} else if ("".equals(cardId) || cardId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_ID_EMPTY.getCode(),
					ExceptionCodeDes.GOODS_ID_EMPTY.getDescription());
		} else if ("".equals(cardNum) || cardNum == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.BUYE_GOODS_NUM_NULL.getCode(),
					ExceptionCodeDes.BUYE_GOODS_NUM_NULL.getDescription());
		} else if ("".equals(sporder_id) || sporder_id == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.COOPERATION_PLATFORM_ORDERNO_NULL
							.getCode(),
					ExceptionCodeDes.COOPERATION_PLATFORM_ORDERNO_NULL
							.getDescription());
		} else if ("".equals(sporder_time) || sporder_time == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.TIME_STAMP_NULL.getCode(),
					ExceptionCodeDes.TIME_STAMP_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	/**
	 * 数据校验
	 */
	private boolean checkDataOfSaveDirectRecharge(OhFeiTradeInfo info,
			String reqId) throws ServerDisponseException {
		boolean flag = false;
		String userId = info.getUserid();
		String userPws = info.getUserpws();
		String cardId = info.getCardid();
		String cardNum = info.getCardnum();
		String sporder_id = info.getSporder_id();
		String sporder_time = info.getSporder_time();
		String game_userid = info.getGame_userid();
		String version = info.getVersion();
		if ("".equals(userId) || userId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.COOPERATION_USERNO_NULL.getCode(),
					ExceptionCodeDes.COOPERATION_USERNO_NULL.getDescription());
		} else if ("".equals(userPws) || userPws == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.USER_PASSWORD_NULL.getCode(),
					ExceptionCodeDes.USER_PASSWORD_NULL.getDescription());
		} else if ("".equals(cardId) || cardId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_ID_EMPTY.getCode(),
					ExceptionCodeDes.GOODS_ID_EMPTY.getDescription());
		} else if ("".equals(cardNum) || cardNum == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.BUYE_GOODS_NUM_NULL.getCode(),
					ExceptionCodeDes.BUYE_GOODS_NUM_NULL.getDescription());
		} else if ("".equals(sporder_id) || sporder_id == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.COOPERATION_PLATFORM_ORDERNO_NULL
							.getCode(),
					ExceptionCodeDes.COOPERATION_PLATFORM_ORDERNO_NULL
							.getDescription());
		} else if ("".equals(sporder_time) || sporder_time == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.TIME_STAMP_NULL.getCode(),
					ExceptionCodeDes.TIME_STAMP_NULL.getDescription());
		} else if ("".equals(game_userid) || game_userid == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GAME_PLAYER_ACCOUNT_NULL.getCode(),
					ExceptionCodeDes.GAME_PLAYER_ACCOUNT_NULL.getDescription());
		} else if ("".equals(version) || version == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.VERSION_NULL.getCode(),
					ExceptionCodeDes.VERSION_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	/**
	 * 数据校验
	 */
	private boolean checkDataOfByPage(OhFeiGoodsBrandInfo info, String reqId)
			throws ServerDisponseException {
		boolean flag = false;
		String currentPage = info.getCurrentPage() + "";
		String pageSize = info.getPageSize() + "";
		if ("".equals(currentPage) || currentPage == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.CURRENT_PAGE_NULL.getCode(),
					ExceptionCodeDes.CURRENT_PAGE_NULL.getDescription());
		} else if ("".equals(pageSize) || pageSize == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.PAGE_SIZE_NULL.getCode(),
					ExceptionCodeDes.PAGE_SIZE_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	/**
	 * 数据校验
	 */
	private boolean checkDataOfBalance(OhFeiGoodsInfo info, String reqId)
			throws ServerDisponseException {
		boolean flag = false;
		String userId = info.getUserid();
		String userPws = info.getUserpws();
		String version = info.getVersion();

		if ("".equals(userId) || userId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.COOPERATION_USERNO_NULL.getCode(),
					ExceptionCodeDes.COOPERATION_USERNO_NULL.getDescription());
		} else if ("".equals(userPws) || userPws == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.USER_PASSWORD_NULL.getCode(),
					ExceptionCodeDes.USER_PASSWORD_NULL.getDescription());
		} else if ("".equals(version) || version == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.VERSION_NULL.getCode(),
					ExceptionCodeDes.VERSION_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	/**
	 * 数据校验
	 */
	private boolean checkDataOfBrand(OhFeiGoodsBrandInfo info, String reqId)
			throws ServerDisponseException {
		boolean flag = false;
		String userId = info.getUserid();
		String userPws = info.getUserpws();
		String cardId = info.getCardid();
		String version = info.getVersion();

		if ("".equals(userId) || userId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.COOPERATION_USERNO_NULL.getCode(),
					ExceptionCodeDes.COOPERATION_USERNO_NULL.getDescription());
		} else if ("".equals(userPws) || userPws == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.USER_PASSWORD_NULL.getCode(),
					ExceptionCodeDes.USER_PASSWORD_NULL.getDescription());
		} else if ("".equals(cardId) || cardId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.BRAND_ID_NULL.getCode(),
					ExceptionCodeDes.BRAND_ID_NULL.getDescription());
		} else if ("".equals(version) || version == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.VERSION_NULL.getCode(),
					ExceptionCodeDes.VERSION_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	/**
	 * 数据校验
	 */
	private boolean checkDataOfGoods(OhFeiGoodsInfo info, String reqId)
			throws ServerDisponseException {
		boolean flag = false;
		String userId = info.getUserid();
		String userPws = info.getUserpws();
		String version = info.getVersion();

		if ("".equals(userId) || userId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.COOPERATION_USERNO_NULL.getCode(),
					ExceptionCodeDes.COOPERATION_USERNO_NULL.getDescription());
		} else if ("".equals(userPws) || userPws == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.USER_PASSWORD_NULL.getCode(),
					ExceptionCodeDes.USER_PASSWORD_NULL.getDescription());
		} else if ("".equals(version) || version == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.VERSION_NULL.getCode(),
					ExceptionCodeDes.VERSION_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	/**
	 * 发送http请求，并对返回的xml格式的结果进行解析
	 * 
	 * @param url
	 * @return
	 * @throws Exception
	 */
	private OhFeiResponseInfo sendHttpRequest(String url) throws Exception {
		OhFeiResponseInfo resInfo = null;
		HttpClient httpClient = new HttpClient();
		try {
			// 供应商返回消息编码方式
			String supplyEncode = "GB2312";
			String xmlStr = httpClient.doGet(url, supplyEncode);
			// 对返回的xml格式字符串数据进行解析
			List<OhFeiResponseInfo> resInfoList = OhFeiResOfBalanceSaxParseXml
					.parseXmlString(xmlStr);
			if (resInfoList.size() > 0) {
				for (int i = 0; i < resInfoList.size(); i++) {
					resInfo = resInfoList.get(i);
				}
				return resInfo;
			}
		} catch (Exception e) {
			log.error(e);
			throw e;
		}

		return resInfo;
	}

	/**
	 * 发送http请求，并对返回的xml格式的结果进行解析
	 * 
	 * @param url
	 * @return
	 */
	private OhFeiResponseInfo sendHttpRequestOfBrand(String url, Head head) {
		OhFeiResponseInfo resInfo = new OhFeiResponseInfo();
		HttpClient httpClient = new HttpClient();
		try {
			// 供应商返回消息编码方式
			String supplyEncode = "GB2312";
			String xmlStr = httpClient.doGet(url, supplyEncode);
			Map<String, Object> map = new HashMap<String, Object>();
			// 对返回的xml格式字符串数据进行解析
			map = OhFeiResOfDomParseXml.parseXmlOfBrandString(xmlStr);
			OhFeiResponseInfo infoError = new OhFeiResponseInfo();
			infoError = (OhFeiResponseInfo) map.get("infoError");
			@SuppressWarnings("unchecked")
			List<OhFeiResponseInfo> resInfoList = (List<OhFeiResponseInfo>) map
					.get("resList");
			if (resInfoList != null && resInfoList.size() > 0) {
				// 把响应的结果插入品牌数据库
				insertGoodsBrand(resInfoList, head);

				// 同步商品品牌的记录数
				int resNum = 0;
				resNum = resInfoList.size();
				resInfo.setResNum(resNum);
			} else {
				resInfo = infoError;
				resInfo.setResNum(0);
			}
		} catch (Exception e) {
			log.error(e);
		}

		return resInfo;
	}

	/**
	 * 发送http请求，并对返回的xml格式的结果进行解析
	 * 
	 * @param url
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private OhFeiGoodsInfo sendHttpRequestOfGoods(String url, Head head) {
		List<OhFeiGoodsInfo> resInfoList = null;
		OhFeiGoodsInfo resInfo = new OhFeiGoodsInfo();
		HttpClient httpClient = new HttpClient();
		try {
			// 供应商返回消息编码方式 GB2312
			String supplyEncode = "GB2312";
			String xmlStr = httpClient.doGet(url, supplyEncode);
			Map<String, Object> map = new HashMap<String, Object>();
			// 对返回的xml格式字符串数据进行解析
			map = OhFeiResOfDomParseXml.parseXmlOfGoodsString(xmlStr);

			OhFeiGoodsInfo infoError = new OhFeiGoodsInfo();
			infoError = (OhFeiGoodsInfo) map.get("infoError");
			resInfoList = (List<OhFeiGoodsInfo>) map.get("resList");
			if (resInfoList != null && resInfoList.size() > 0) {
				// 把响应的结果插入数据库
				insertGoods(resInfoList, head);

				// 同步商品记录数
				int resNum = 0;
				resNum = resInfoList.size();
				String retCode = resInfoList.get(0).getRetcode();
				resInfo.setResNum(resNum);
				resInfo.setRetcode(retCode);
			} else {
				resInfo = infoError;
				resInfo.setResNum(0);
			}
		} catch (Exception e) {
			log.error(e);
		}

		return resInfo;
	}

	/**
	 * 发送http请求，并对返回的xml格式的结果进行解析
	 * 
	 * @param url
	 * @return
	 */
	private List<OhFeiGoodsAreaServerInfo> sendHttpRequestOfAreaServer(
			String url) {
		List<OhFeiGoodsAreaServerInfo> resInfoList = null;
		HttpClient httpClient = new HttpClient();
		try {
			// 供应商返回消息编码方式
			String supplyEncode = "GB2312";
			String xmlStr = httpClient.doGet(url, supplyEncode);
			// 对返回的xml格式字符串数据进行解析
			resInfoList = OhFeiResOfDomParseXml
					.parseXmlOfAreaServerString(xmlStr);

		} catch (Exception e) {
			log.error(e);
		}

		return resInfoList;
	}

	/**
	 * 发送http请求，并对返回的xml格式的结果进行解析
	 * 
	 * @param url
	 * @return
	 */
	private List<OhFeiTradeInfo> sendHttpRequestOfExtractCard(String url) {
		List<OhFeiTradeInfo> resInfoList = null;
		HttpClient httpClient = new HttpClient();
		try {
			// 供应商返回消息编码方式
			String supplyEncode = "GB2312";
			String xmlStr = httpClient.doGet(url, supplyEncode);
			resInfoList = OhFeiResOfDomParseXml
					.parseXmlOfCardTradeString(xmlStr);
		} catch (Exception e) {
			log.error(e);
		}

		return resInfoList;
	}

	/**
	 * 发送http请求，并对返回的xml格式的结果进行解析
	 * 
	 * @param url
	 * @return
	 */
	private OhFeiTradeInfo sendHttpRequestOfDirectRecharge(String url) {
		OhFeiTradeInfo resInfo = null;
		HttpClient httpClient = new HttpClient();
		try {
			// 供应商返回消息编码方式
			String supplyEncode = "GB2312";
			String xmlStr = httpClient.doGet(url, supplyEncode);
			resInfo = OhFeiResOfDomParseXml
					.parseXmlOfDirectRechargeString(xmlStr);

		} catch (Exception e) {
			log.error(e);
		}

		return resInfo;
	}

	/**
	 * http请求url所传参数字符串拼接
	 * 
	 * @param info
	 * @return
	 */
	private String assembleParaOfBalanceStr(OhFeiGoodsInfo info) {
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("userid=");
		strBuf.append(info.getUserid());
		strBuf.append("&");
		strBuf.append("userpws=");
		// 对密码进行MD5加密
		String userPws = info.getUserpws();
		String userPwsMD5 = UtilMD5Encryption.createMD5Encryption(userPws);

		strBuf.append(userPwsMD5);
		strBuf.append("&");
		strBuf.append("version=");
		strBuf.append(info.getVersion());

		String paraStr = strBuf.toString();
		return paraStr;
	}

	/**
	 * http请求url所传参数字符串拼接
	 * 
	 * @param info
	 * @return
	 */
	private String assembleParaOfBrandStr(OhFeiGoodsBrandInfo info) {
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("userid=");
		strBuf.append(info.getUserid().trim());
		strBuf.append("&");
		strBuf.append("userpws=");
		// 对密码进行MD5加密
		String userPws = info.getUserpws().trim();
		String userPwsMD5 = UtilMD5Encryption.createMD5Encryption(userPws);

		strBuf.append(userPwsMD5);
		strBuf.append("&");
		strBuf.append("cardid=");
		strBuf.append(info.getCardid().trim());
		strBuf.append("&");
		strBuf.append("version=");
		strBuf.append(info.getVersion().trim());

		String paraStr = strBuf.toString();
		return paraStr;
	}

	/**
	 * http请求url所传参数字符串拼接
	 * 
	 * @param info
	 * @return
	 */
	private String assembleParaOfGoods(OhFeiGoodsInfo info) {
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("userid=");
		strBuf.append(info.getUserid().trim());
		strBuf.append("&");
		strBuf.append("userpws=");
		// 对密码进行MD5加密
		String userPws = info.getUserpws().trim();
		String userPwsMD5 = UtilMD5Encryption.createMD5Encryption(userPws);

		strBuf.append(userPwsMD5);
		strBuf.append("&");
		strBuf.append("cardid=");
		strBuf.append(info.getCardid().trim());
		strBuf.append("&");
		strBuf.append("version=");
		strBuf.append(info.getVersion().trim());

		String paraStr = strBuf.toString().trim();
		return paraStr;
	}

	/**
	 * http请求url所传参数字符串拼接
	 * 
	 * @param info
	 * @return
	 */
	private String assembleParaOfAreaServer(OhFeiGoodsAreaServerInfo info) {
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("gameid=");
		strBuf.append(info.getGameId());

		String paraStr = strBuf.toString();
		return paraStr;
	}

	/**
	 * http请求url所传参数字符串拼接
	 * 
	 * @param info
	 * @return
	 */
	private String assembleParaOfExtractCard(OhFeiTradeInfo info) {
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("userid=");
		strBuf.append(info.getUserid());
		strBuf.append("&");
		strBuf.append("userpws=");
		// 对密码进行MD5加密
		String userPws = info.getUserpws();
		String userPwsMD5 = UtilMD5Encryption.createMD5Encryption(userPws);

		strBuf.append(userPwsMD5);
		strBuf.append("&");
		strBuf.append("cardid=");
		strBuf.append(info.getCardid());
		strBuf.append("&");
		strBuf.append("cardnum=");
		strBuf.append(info.getCardnum());
		strBuf.append("&");
		strBuf.append("sporder_id=");
		strBuf.append(info.getSporder_id());
		strBuf.append("&");
		strBuf.append("sporder_time=");
		strBuf.append(info.getSporder_time());
		strBuf.append("&");
		strBuf.append("md5_str=");

		// md5_str检验码的计算方法
		String data = info.getUserid() + userPwsMD5 + info.getCardid()
				+ info.getCardnum() + info.getSporder_id()
				+ info.getSporder_time();
		String md5_str = UtilMD5Encryption.createMD5Encryption(data);

		strBuf.append(md5_str);
		strBuf.append("&");
		strBuf.append("version=");
		strBuf.append(info.getVersion());

		String paraStr = strBuf.toString();
		return paraStr;
	}

	/**
	 * http请求url所传参数字符串拼接
	 * 
	 * @param info
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	private String assembleParaOfDirectRecharge(OhFeiTradeInfo info) {
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("userid=");
		strBuf.append(info.getUserid());
		strBuf.append("&");
		strBuf.append("userpws=");
		// 对密码进行MD5加密
		String userPws = info.getUserpws();
		String userPwsMD5 = UtilMD5Encryption.createMD5Encryption(userPws);

		strBuf.append(userPwsMD5);
		strBuf.append("&");
		strBuf.append("cardid=");
		strBuf.append(info.getCardid());
		strBuf.append("&");
		strBuf.append("cardnum=");
		strBuf.append(info.getCardnum());
		strBuf.append("&");
		strBuf.append("sporder_id=");
		strBuf.append(info.getSporder_id());
		strBuf.append("&");
		strBuf.append("sporder_time=");
		strBuf.append(info.getSporder_time());
		strBuf.append("&");
		strBuf.append("game_userid=");
		strBuf.append(info.getGame_userid());
		strBuf.append("&");
		strBuf.append("game_area=");

		String game_areaStr = info.getGame_area();
		String game_area = "";
		if (!("".equals(game_areaStr)) && game_areaStr != null) {
			try {
				game_area = URLEncoder.encode(game_areaStr, "GBK"); // GBK编码
			} catch (UnsupportedEncodingException e) {
				log.error(e);
			}
		}

		strBuf.append(game_area);
		strBuf.append("&");
		strBuf.append("game_srv=");

		String game_srvStr = info.getGame_srv();
		String game_srv = "";
		if (!("".equals(game_srvStr)) && game_srvStr != null) {
			try {
				game_srv = URLEncoder.encode(game_srvStr, "GBK"); // GBK编码
			} catch (UnsupportedEncodingException e) {
				log.error(e);
			}
		}

		strBuf.append(game_srv);
		strBuf.append("&");
		strBuf.append("md5_str=");

		// md5_str检验码的计算方法
		String data = info.getUserid() + userPwsMD5 + info.getCardid()
				+ info.getCardnum() + info.getSporder_id()
				+ info.getSporder_time() + info.getGame_userid()
				+ info.getGame_area() + info.getGame_srv();
		String md5_str = UtilMD5Encryption.createMD5Encryption(data);
		strBuf.append(md5_str);
		strBuf.append("&");
		strBuf.append("version=");
		strBuf.append(info.getVersion());

		String paraStr = strBuf.toString();
		return paraStr;
	}

	/**
	 * 把响应的结果插入品牌数据库
	 * 
	 * @param resInfo
	 * @param head
	 * @throws ServerDisponseException
	 */
	private void insertGoodsBrand(List<OhFeiResponseInfo> resInfoList, Head head)
			throws ServerDisponseException {
		for (OhFeiResponseInfo resInfo : resInfoList) {
			OhFeiGoodsBrandInfo info = new OhFeiGoodsBrandInfo();
			info.setClassid(resInfo.getClassid());
			info.setCardid(resInfo.getCardid());
			info.setCardname(resInfo.getCardname());
			info.setDetail(resInfo.getDetail());
			info.setCompty(resInfo.getCompty());
			info.setUsecity(resInfo.getUsecity());
			info.setUsemethod(resInfo.getUsemethod());
			info.setFullcostsite(resInfo.getFullcostsite());
			info.setProare(resInfo.getProare());
			info.setServiceNum(resInfo.getServiceNum());
			// 记录操作时间
			String nowTime = ServerDateAndTime.getNowDateTimeOfFormat();
			info.setOperateTime(nowTime);

			OhFeiGoodsBrandInfo brandInfo = null;
			String cardid = resInfo.getCardid();
			brandInfo = ohFeiGoodsBrandDAO.queryByCardId(cardid);
			if (brandInfo == null) { // 新增
				info.setId(UUID.randomUUID().toString());
				try {
					ohFeiGoodsBrandDAO.insert(info);
				} catch (Exception e) {
					log.error(e);
					throw new ServerDisponseException(
							ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
							ExceptionCodeDes.ACCESS_DATABASE_ERROR
									.getDescription());
				}
			} else { // 更新
				info.setId(brandInfo.getId());
				try {
					ohFeiGoodsBrandDAO.update(info);
				} catch (Exception e) {
					log.error(e);
					throw new ServerDisponseException(
							ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
							ExceptionCodeDes.ACCESS_DATABASE_ERROR
									.getDescription());
				}
			}
		}
	}

	/**
	 * 把响应的结果插入数据库
	 * 
	 * @param resInfo
	 * @param head
	 * @throws ServerDisponseException
	 */
	private void insertGoods(List<OhFeiGoodsInfo> resInfoList, Head head)
			throws ServerDisponseException {

		for (OhFeiGoodsInfo resInfo : resInfoList) {
			OhFeiGoodsInfo info = new OhFeiGoodsInfo();

			// 根据原始商品编号，查看该产品是否存在
			String goodsId = resInfo.getCardid();
			OhFeiGoodsInfo ohFeiGoodsInfo = null;
			ohFeiGoodsInfo = ohFeiGoodsInfoDAO.queryById(goodsId);

			info.setGoodsId(goodsId);
			info.setGoodsName(resInfo.getGoodsName());
			info.setGoodsAlias(resInfo.getGoodsAlias());
			info.setParValue(resInfo.getParValue());
			info.setGoodsInPrice(resInfo.getGoodsInPrice());
			info.setAmounts(resInfo.getAmounts());
			info.setSubClassId(resInfo.getSubClassId());
			info.setClassType(resInfo.getClassType());
			info.setFullCostsite(resInfo.getFullCostsite());
			info.setGameArea(resInfo.getGameArea());
			info.setGameServers(resInfo.getGameServers());

			// 记录插入（更新）时间
			String now = ServerDateAndTime.getNowDateTimeOfFormat();
			info.setImportTime(now);
			info.setIsAudit("0");
			info.setCaption(resInfo.getCaption());
			info.setAccountDesc(resInfo.getAccountDesc());
			info.setSlsGoodsId(resInfo.getSlsGoodsId());
			info.setInPrice(resInfo.getInPrice());
			info.setSysddPrice(resInfo.getSysddPrice());
			info.setSysdd1Price(resInfo.getSysdd1Price());
			info.setSysdd2Price(resInfo.getSysdd2Price());
			info.setMemberPrice(resInfo.getMemberPrice());

			if (ohFeiGoodsInfo == null) { // 不存在：进行插入
				info.setId(UUID.randomUUID().toString());

				try {
					ohFeiGoodsInfoDAO.insert(info);
				} catch (Exception e) {
					log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！");
					throw new ServerDisponseException(
							ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
							ExceptionCodeDes.ACCESS_DATABASE_ERROR
									.getDescription());
				}

			} else { // 存在：进行更新
				info.setId(ohFeiGoodsInfo.getId());

				try {
					ohFeiGoodsInfoDAO.update(info);
				} catch (Exception e) {
					log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！");
					throw new ServerDisponseException(
							ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
							ExceptionCodeDes.ACCESS_DATABASE_ERROR
									.getDescription());
				}
			}
		}
	}

	/**
	 * 把响应的结果插入殴飞区服表
	 * 
	 * @param resInfo
	 * @param head
	 * @throws ServerDisponseException
	 */
	private void insertAreaServer(List<OhFeiGoodsAreaServerInfo> resInfoList,
			Head head) throws ServerDisponseException {
		for (OhFeiGoodsAreaServerInfo resInfo : resInfoList) {
			OhFeiGoodsAreaServerInfo info = new OhFeiGoodsAreaServerInfo();

			// 根据sx编号，查看该区服信息是否存在
			String sx = resInfo.getSx();
			OhFeiGoodsAreaServerInfo info1 = null;
			info1 = ohFeiGoodsAreaServerDAO.queryBySx(sx);

			info.setGameId(resInfo.getGameId());
			info.setGameName(resInfo.getGameName());
			info.setArea(resInfo.getArea());
			info.setServer(resInfo.getServer());
			info.setSx(resInfo.getSx());
			info.setAreaFlag(resInfo.getAreaFlag());
			info.setServerFlag(resInfo.getServerFlag());

			if (info1 == null) { // 不存在：进行插入
				info.setId(UUID.randomUUID().toString());
				info.setInsertTime(ServerDateAndTime.getNowDateTimeOfFormat());
				try {
					ohFeiGoodsAreaServerDAO.insert(info);
				} catch (Exception e) {
					log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！");
					throw new ServerDisponseException(
							ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
							ExceptionCodeDes.ACCESS_DATABASE_ERROR
									.getDescription());
				}

			} else {
				info.setId(info1.getId());
				info.setModifyTime(ServerDateAndTime.getNowDateTimeOfFormat());
				try {
					ohFeiGoodsAreaServerDAO.update(info);
				} catch (Exception e) {
					log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！");
					throw new ServerDisponseException(
							ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
							ExceptionCodeDes.ACCESS_DATABASE_ERROR
									.getDescription());
				}
			}
		}
	}

	@Override
	public List<OhFeiServerInfo> queryOhFlyUserInfo(Head head)
			throws ServerDisponseException {
		List<OhFeiServerInfo> serverList = OhFeiServerSaxParseXml
				.parseXmlString("conf" + File.separator + "ohFeiServer.xml");
		if (serverList != null && serverList.size() > 0) {
			return serverList;
		} else {
			throw new ServerDisponseException(
					ExceptionCodeDes.PARSE_OHFLY_USER_ERROR.getCode(),
					ExceptionCodeDes.PARSE_OHFLY_USER_ERROR.getDescription());
		}
	}

	/**
	 * 殴飞订单处理结果通知(订单结果异步通知)
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String updateOhFeiOrderStatus(String body, Head head)
			throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		OhFeiOrderStatusInfo info = gson.fromJson(body,
				OhFeiOrderStatusInfo.class);
		boolean flag = checkDataOfOhFeiStatus(info);
		if (flag) {
			// 订单处理结果主动给下游
			String userhandleResult = sendUserOrderStatusMsg(info, head);
			// 更新订单表
			updateGoodsorder(head, info, userhandleResult);
		}

		return null;
	}

	/**
	 * 主动给下游(如果下游未收到消息)
	 * 
	 * @param info
	 */
	private String sendUserOrderStatusMsg(OhFeiOrderStatusInfo info, Head head) {
		String handleResult = "FAILED";
		// 下游回调地址
		String notifyUrl = null;
		// 下游用户编号
		String contractId = null;
		String orderId = info.getSporder_id();
		GoodsOrderInfo goodsOrderInfo = null;
		goodsOrderInfo = goodsOrderDAO.queryNotifyUrlByOrderId(orderId);
		if (goodsOrderInfo != null) {
			notifyUrl = goodsOrderInfo.getNotifyUrl();
			contractId = goodsOrderInfo.getContractId();
		}
		String secretKey = null; // 下游用户密钥
		DownstreamUserInfo downstreamUserInfo = null;
		downstreamUserInfo = downstreamUserDAO.queryUserByUserNO(contractId);
		if (downstreamUserInfo != null) {
			secretKey = downstreamUserInfo.getSecretKey();
		}

		String parameter = obtainParameter(info, goodsOrderInfo, secretKey);
		String url = notifyUrl + "?" + parameter;
		HttpClient httpClient = new HttpClient();
		// 供应商返回消息编码方式
		String supplyEncode = "UTF-8";
		// 给下游发消息
		try {
			log.info("请求ID：" + head.getRequestId() + "，手拉手异步回调给下游用户的url："
					+ url);
			String xmlStr = httpClient.doGet(url, supplyEncode);
			log.info("请求ID：" + head.getRequestId() + "，下游用户的返回结果："
					+ xmlStr);
			if (xmlStr != null && !("".equals(xmlStr))) {
				SLSHttpInterfaceInfo slsInfo = null;
				slsInfo = OrderStatusSendParse.readUserSendMsg(xmlStr);
				if (slsInfo != null) {
					handleResult = slsInfo.getHandleResult();
				}
			}
		} catch (Exception e) {
			log.error(e);
		}

		return handleResult;
	}

	/**
	 * 创建参数字符串
	 * 
	 * @param info
	 * @param goodsOrderInfo
	 * @return
	 */
	private String obtainParameter(OhFeiOrderStatusInfo info,
			GoodsOrderInfo goodsOrderInfo, String secretKey) {
		// 下游用户订单号
		String ptOrderNo = goodsOrderInfo.getMerchantOrderId();
		// 手拉手订单号
		String supOrderNo = info.getSporder_id();
		// 充值后状态
		String ret_code = info.getRet_code();
		// 成功处理时间
		String ordersuccesstime = info.getOrdersuccesstime();
		// 失败原因
		String err_msg = info.getErr_msg();

		String logCodmsg = null;
		if ("1".equals(ret_code)) { // 成功
			logCodmsg = "成功";
		} else if ("9".equals(ret_code)) { // 失败
			logCodmsg = "失败";
		}
		log.info("殴飞异步推送过来的订单处理结果：  \n手拉手订单号:" + supOrderNo + "充值后状态:"
				+ logCodmsg + "处理时间:" + ordersuccesstime + "失败原因:" + err_msg);

		StringBuilder strBui = new StringBuilder();
		strBui.append("ptOrderNo=");
		strBui.append(ptOrderNo);
		strBui.append("&");
		strBui.append("supOrderNo=");
		strBui.append(supOrderNo);
		strBui.append("&");
		strBui.append("status=");
		String status = null;
		if ("1".equals(ret_code)) { // 成功
			status = "ORDER_SUCCESS";
		} else if ("9".equals(ret_code)) { // 失败
			status = "ORDER_FAILED";
		}
		strBui.append(status);
		strBui.append("&");

		if ("ORDER_SUCCESS".equals(status)) {
			strBui.append("supOrderSuccessTime=");
			strBui.append(ordersuccesstime);
			strBui.append("&");
		} else {
			strBui.append("failedReason=");
			try {
				if (err_msg != null && !("".equals(err_msg))) {
					err_msg = URLEncoder.encode(err_msg, "UTF-8");
				}
			} catch (UnsupportedEncodingException e) {
				log.error(e);
			}
			strBui.append(err_msg);
			strBui.append("&");
		}

		strBui.append("sign=");
		String sign = createSign(ptOrderNo, supOrderNo, status,
				ordersuccesstime, err_msg, secretKey);
		strBui.append(sign);
		log.info("异步发送给下游用户的订单处理结果    \n 合作方订单编号:" + ptOrderNo + "\n 本地订单编号:"
				+ supOrderNo + "\n 订单最终处理结果：" + status + "\n订单处理的时间:"
				+ ordersuccesstime + "\n失败原因：" + err_msg);

		String result = strBui.toString().trim();
		return result;
	}

	/**
	 * 创建签名字符串
	 * 
	 * @param ptOrderNo
	 * @param supOrderNo
	 * @param status
	 * @param supOrderSuccessTime
	 * @return
	 */
	private String createSign(String ptOrderNo, String supOrderNo,
			String status, String supOrderSuccessTime, String err_msg,
			String secretKey) {
		StringBuilder strBui = new StringBuilder();
		strBui.append(ptOrderNo);
		strBui.append(supOrderNo);
		strBui.append(status);
		if (supOrderSuccessTime != null && !("".equals(supOrderSuccessTime))) {
			strBui.append(supOrderSuccessTime);
		}
		if (err_msg != null && !("".equals(err_msg))) {
			strBui.append(err_msg);
		}

		strBui.append(secretKey);
		String data = strBui.toString().trim();
		String result = null;
		result = UtilMD5Encryption.createMD5Encryption(data);

		return result;
	}

	/**
	 * 根据交易返回的结果更新订单表
	 * 
	 * @param head
	 * 
	 * @param info
	 * 
	 */
	private void updateGoodsorder(Head head, OhFeiOrderStatusInfo info,
			String userhandleResult) {
		// 充值后状态
		String ret_code = info.getRet_code();
		// sls订单号
		String sporder_id = info.getSporder_id();
		// 成功处理时间
		String ordersuccesstime = info.getOrdersuccesstime();
		// 失败原因
		String err_msg = info.getErr_msg();
		GoodsOrderInfo goodsOrderInfo = new GoodsOrderInfo();
		goodsOrderInfo.setOrderId(sporder_id);
		goodsOrderInfo.setResult(ret_code);

		String statusValue = "2";
		String result = null;
		if ("1".equals(ret_code)) { // 成功
			statusValue = "1";
			result = "ORDER_SUCCESS";
		} else if ("9".equals(ret_code)) { // 失败
			statusValue = "0";
			result = "ORDER_FAILED";

			// 退款操作
			DeductionRefundInfo dedInfo = null;
			dedInfo = DeductionRefund.refundWhenAsynCallBackFailed(sporder_id,
					goodsOrderDAO, head);
			String paymentStatus = "";
			if (dedInfo != null) {
				paymentStatus = dedInfo.getPaymentStatus();
			}

			if ("1".equals(paymentStatus)) { // 成功
				goodsOrderInfo.setIsPay("3"); // 退款成功

				// 退款成功，通知下游用户
				DeductionRefund.noticeUserRefundMsg(sporder_id, dedInfo,
						goodsOrderDAO, downstreamUserDAO, goodsOrderLogDAO);

			} else if ("2".equals(paymentStatus)) {
				GoodsOrderInfo orderInfo = goodsOrderDAO
						.queryByOrderId(sporder_id);
				if (orderInfo != null) {
					String isPayValue = orderInfo.getIsPay(); // 支付状态值
					if ("1".equals(isPayValue)) {
						goodsOrderInfo.setIsPay("2"); // 退款失败
					}
				}
			}
		}
		goodsOrderInfo.setStatus(statusValue);
		goodsOrderInfo.setResult(result);
		goodsOrderInfo.setSupOrderSuccessTime(ordersuccesstime);
		goodsOrderInfo.setStatusRemark(err_msg);
		goodsOrderInfo.setUserhandleResult(userhandleResult);
		goodsOrderInfo.setSynOrAsy("1"); // 异步推送
		// 数据更新
		try {
			goodsOrderDAO.update(goodsOrderInfo);
		} catch (Exception e) {
			log.error(e);
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}

		// 写订单日志
		try {
			String orderId = sporder_id;
			GoodsOrderInfo goodsOrderInfo1 = new GoodsOrderInfo();
			goodsOrderInfo1 = this.goodsOrderDAO.queryByOrderId(orderId);
			if (goodsOrderInfo1 != null) {
				GoodsOrderLog orderLog = Helper.copy(goodsOrderInfo1);
				orderLog.setId(UUID.randomUUID().toString());
				orderLog.setModifyTime(ServerDateAndTime
						.getNowDateTimeOfFormat());
				this.goodsOrderLogDAO.insert(orderLog);
			}

		} catch (Exception e) {
			log.error(e.toString());
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}

	}

	/**
	 * 
	 * @param ret_code
	 * @param sporder_id
	 * @param ordersuccesstime
	 * @param err_msg
	 * @return
	 * @throws IOException
	 */
	private boolean checkDataOfOhFeiStatus(OhFeiOrderStatusInfo info) {
		boolean flag = false;
		// 充值后状态
		String ret_code = info.getRet_code();
		// sls订单号
		String sporder_id = info.getSporder_id();
		if ("".equals(ret_code) || ret_code == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.ORDER_STATUS_NULL.getCode(),
					ExceptionCodeDes.ORDER_STATUS_NULL.getDescription());
		} else if ("".equals(sporder_id) || sporder_id == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.SLS_ORDERID_NULL.getCode(),
					ExceptionCodeDes.SLS_ORDERID_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

}
